﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using KeanuMvc.Reference;
using HISModels.Entities;
using System.Net.Http;
using HISModels.ViewModel;

namespace HISRestServices.Services {

    [ServiceContract()]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class Ref : BaseServices, IDisposable {

        #region Field
        private HttpStatusCode statusCode = new HttpStatusCode();
        private string detailMessage = string.Empty;
        #endregion

        #region Area
        [WebInvoke(Method = "POST", UriTemplate = "area/paging/")]
        public HttpResponseMessage<DataTablesNet> GetAreaAllPost(PostDataTablesNet pagingSearch) {
            HttpResponseMessage<DataTablesNet> returnValue = null;
            DataTablesNet paging = null;

            string[] param = null;


            string area = string.Empty;
            string city = string.Empty;
            string province = string.Empty;
            int countryID = 100; //set default to Indonesia

            try {
                paging = new DataTablesNet();

                paging.sEcho = pagingSearch.sEcho;

                param = pagingSearch.sSearch.Split(';');


                area = param[0].Split('=')[1].NullSafeToString().Trim();
                city = param[1].Split('=')[1].NullSafeToString().Trim();
                province = param[2].Split('=')[1].NullSafeToString().Trim();
                countryID = param[3].Split('=')[1].NullSafeToString() != string.Empty ? Convert.ToInt32(param[3].Split('=')[1]) : countryID; //set default to Indoensia

                var a = from b in AppContext.RefAreas
                        join c in AppContext.RefCities on b.CityId equals c.Id
                        join p in AppContext.RefProvinces on c.ProvinceId equals p.Id
                        join d in AppContext.RefCountries on p.CountryId equals d.Id
                        select new {
                            AreaId = b.Id,
                            b.AreaCode,
                            b.AreaName,
                            CityId = c.Id,
                            CityCode = c.CodeValue,
                            CityName = c.NameValue,
                            CountryId = d.Id,
                            CountryName = d.NameValue,
                            ProvinceId = p.Id,
                            ProvinceName = p.NameValue
                        };
                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.CityName.Contains(city) && p.AreaName.Contains(area) && p.ProvinceName.Contains(province) && p.CountryId == countryID);


                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.AreaId);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;

                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }

            returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
            returnValue.Content = new ObjectContent<DataTablesNet>(paging);
            if (paging == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            return returnValue;
        }
        #endregion

        #region City
        [WebInvoke(Method = "POST", UriTemplate = "city/paging/")]
        public HttpResponseMessage<DataTablesNet> GetCityAllPost(PostDataTablesNet pagingSearch) {
            HttpResponseMessage<DataTablesNet> returnValue = null;
            DataTablesNet paging = null;
            string[] param = null;

            string code = string.Empty;
            string city = string.Empty;
            string state = string.Empty;
            string country = string.Empty;

            try {
                paging = new DataTablesNet();
                paging.sEcho = pagingSearch.sEcho;

                param = pagingSearch.sSearch.Split(';');

                code = param[0].Split('=')[1].NullSafeToString().Trim();
                city = param[1].Split('=')[1].NullSafeToString().Trim();
                state = param[2].Split('=')[1].NullSafeToString().Trim();
                country = param[3].Split('=')[1].NullSafeToString().Trim();

                var a = from b in AppContext.RefCities
                        join p in AppContext.RefProvinces on b.ProvinceId equals p.Id
                        join c in AppContext.RefCountries on p.CountryId equals c.Id
                        select new {
                            b.Id,
                            b.CodeValue,
                            b.NameValue,
                            CountryName = c.NameValue
                        };

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.CodeValue.Contains(code) && p.NameValue.Contains(city) && p.CountryName.Contains(country));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.Id);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }

            returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
            returnValue.Content = new ObjectContent<DataTablesNet>(paging);
            if (paging == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            return returnValue;
        }
        #endregion

        #region Country
        [WebGet(UriTemplate = "country/")]
        public HttpResponseMessage<List<RefCountry>> GetCountryAll() {
            HttpResponseMessage<List<RefCountry>> returnValue = null;
            List<RefCountry> listCountry = null;
            try {
                listCountry = AppContext.RefCountries.ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            returnValue = new HttpResponseMessage<List<RefCountry>>(statusCode);
            returnValue.Content = new ObjectContent<List<RefCountry>>(listCountry);
            if (listCountry == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            return returnValue;
        }

        [WebGet(UriTemplate = "country/{id}")]
        public HttpResponseMessage<RefCountry> GetCountryDefault(int id) {
            HttpResponseMessage<RefCountry> returnValue = null;
            RefCountry country = null;
            try {
                country = AppContext.RefCountries.Where(c => c.Id == id).First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            returnValue = new HttpResponseMessage<RefCountry>(statusCode);
            returnValue.Content = new ObjectContent<RefCountry>(country);
            if (country == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            return returnValue;
        }
        #endregion

        #region Province
        [WebGet(UriTemplate = "provincebycountry/{id}")]
        public HttpResponseMessage<List<RefProvince>> GetProvinceByCountryId(int id) {
            HttpResponseMessage<List<RefProvince>> returnValue = null;
            List<RefProvince> listProvince = null;
            try {
                listProvince = AppContext.RefProvinces.Where(p => p.Id == id).ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            returnValue = new HttpResponseMessage<List<RefProvince>>(statusCode);
            returnValue.Content = new ObjectContent<List<RefProvince>>(listProvince);
            if (listProvince == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            return returnValue;
        }
        #endregion

        #region Ethnic
        [WebGet(UriTemplate = "ethnic/")]
        public HttpResponseMessage<List<RefEthnicity>> GetListEthnics() {
            HttpResponseMessage<List<RefEthnicity>> returnValue = null;
            List<RefEthnicity> listEthicity = null;
            try {
                listEthicity = (from h in AppContext.RefEthnicities select h).ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<RefEthnicity>>(statusCode);
                returnValue.Content = new ObjectContent<List<RefEthnicity>>(listEthicity);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }
        #endregion

        #region Hospital
        [WebGet(UriTemplate = "hospital/")]
        public HttpResponseMessage<List<RefHu>> GetListHospital() {
            HttpResponseMessage<List<RefHu>> returnValue = null;
            List<RefHu> listHospitalUnit = null;
            try {
                listHospitalUnit = (from h in AppContext.RefHus select h).ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }

            returnValue = new HttpResponseMessage<List<RefHu>>(statusCode);
            returnValue.Content = new ObjectContent<List<RefHu>>(listHospitalUnit);
            if (listHospitalUnit == null)
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "hospital/paging/")]
        public HttpResponseMessage<DataTablesNet> GetHospitalPaging(PostDataTablesNet pagingSearch) {
            DataTablesNet paging = null;
            HttpResponseMessage<DataTablesNet> returnValue = null;
            string[] data = null;

            string code = string.Empty;
            string name = string.Empty;


            try {
                paging = new DataTablesNet();
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');

                code = data[0].Split('=')[1].NullSafeToString().Trim();
                name = data[1].Split('=')[1].NullSafeToString().Trim();


                var a = from b in AppContext.RefHus
                        join c in AppContext.Addresses on b.Id equals c.HospitalId
                        where b.RecordStatus == 1
                        select new {
                            b.Id,
                            b.CodeValue,
                            b.NameValue,
                            b.DescValue,
                            b.Phone,
                            b.CEO,
                            AddressId = c.Id,
                            c.AddressDesc
                        };

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.NameValue.Contains(name) && p.CodeValue.Contains(code));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.Id);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }

            returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
            returnValue.Content = new ObjectContent<DataTablesNet>(paging);

            if (paging == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            return returnValue;
        }

        [WebGet(UriTemplate = "hospital/lob/{id}")]
        public HttpResponseMessage<List<HuLobRel>> GetLobHospitalByHuId(int id) {
            HttpResponseMessage<List<HuLobRel>> returnValue = null;
            List<HuLobRel> listhuLobRel = null;
            try {
                listhuLobRel = (from h in AppContext.RefLobs
                                join r in AppContext.HuLobRels on h.Id equals r.LineOffBusinessUnitID
                                join l in AppContext.RefHus on r.HospitalUnitId equals l.Id
                                where r.HospitalUnitId == id
                                select r).ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<HuLobRel>>(statusCode);
                returnValue.Content = new ObjectContent<List<HuLobRel>>(listhuLobRel);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "hospital/lob/")]
        public HttpResponseMessage<List<HuLobRel>> InsertHospitalLobRel(List<HuLobRel> paramObject) {
            HttpResponseMessage<List<HuLobRel>> returnValue = null;
            try {
                foreach (var item in paramObject) {
                    AppContext.Entry(item).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                }

                detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<HuLobRel>>(statusCode);
                returnValue.Content = new ObjectContent<List<HuLobRel>>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }


        [WebInvoke(Method = "POST", UriTemplate = "hospital/")]
        public HttpResponseMessage<RefHu> InsertHospital(RefHu paramObject) {
            HttpResponseMessage<RefHu> returnValue = null;
            string MessageKey = string.Empty;
            try {
                if (AppContext.RefHus.Where(d => d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Name of Hospital is already exist in Database!";
                    paramObject = null;
                } else {
                    paramObject.RecordStatus = 1;
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;

                    foreach (var addr in paramObject.Addresses) {
                        AppContext.Entry(addr).State = System.Data.EntityState.Added;
                    }

                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefHu>(statusCode);
                returnValue.Content = new ObjectContent<RefHu>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "hospital/")]
        public HttpResponseMessage<RefHu> UpdateHospital(RefHu paramObject) {
            HttpResponseMessage<RefHu> returnValue = null;
            try {
                if (AppContext.RefHus.Where(d => d.Id != paramObject.Id && d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Name of Hospital is already exist in Database!";
                    paramObject = null;
                } else {
                    foreach (var addr in paramObject.Addresses) {
                        AppContext.Entry(addr).State = System.Data.EntityState.Modified;
                    }

                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefHu>(statusCode);
                returnValue.Content = new ObjectContent<RefHu>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "hospital/")]
        public HttpResponseMessage<RefHu> RemoveHospital(int id) {
            HttpResponseMessage<RefHu> returnValue = null;
            RefHu hospital = null;
            try {
                hospital = AppContext.RefHus.Where(d => d.Id == id).First();
                hospital.RecordStatus = 0;
                AppContext.Entry(hospital).State = System.Data.EntityState.Modified;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefHu>(statusCode);
                returnValue.Content = new ObjectContent<RefHu>(hospital);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebGet(UriTemplate = "hospital/role/{id}")]
        public HttpResponseMessage<RefHu> GetHospitalByRoleId(int id) {
            HttpResponseMessage<RefHu> returnValue = null;
            RefHu RefHospitalUnit = null;
            try {
                RefHospitalUnit = (from h in AppContext.RefHus
                                   join r in AppContext.SysRoles on h.Id equals r.HospitalUnitId
                                   where r.RoleId == id
                                   select h)
                                    .First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefHu>(statusCode);
                returnValue.Content = new ObjectContent<RefHu>(RefHospitalUnit);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebGet(UriTemplate = "hospital/{id}")]
        public HttpResponseMessage<RefHu> GetHospitalById(int id) {
            HttpResponseMessage<RefHu> returnValue = null;
            RefHu RefHospitalUnit = null;
            try {
                RefHospitalUnit = (from h in AppContext.RefHus.Include("Addresses")
                                   where h.Id == id
                                   select h)
                                   .First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefHu>(statusCode);
                returnValue.Content = new ObjectContent<RefHu>(RefHospitalUnit);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebGet(UriTemplate = "hospital/room/")] // TODO: will remove if this method wasn't use
        public HttpResponseMessage<List<HuRoom>> GetRoom() {
            HttpResponseMessage<List<HuRoom>> returnValue = null;
            List<HuRoom> listRoom = null;
            RefHu hospital = Hospital;
            try {
                listRoom = (from b in AppContext.HuRooms
                            join d in AppContext.HuWards on b.WardId equals d.Id
                            join c in AppContext.RefHus on d.HospitalUnitId equals c.Id
                            where c.Id == hospital.Id
                            select b).ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            returnValue = new HttpResponseMessage<List<HuRoom>>(statusCode);
            returnValue.Content = new ObjectContent<List<HuRoom>>(listRoom);
            if (listRoom == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "hospital/room/lob/")]
        public HttpResponseMessage<DataTablesNet> GetHospitalRoomByHuId(PostDataTablesNet pagingSearch) {
            DataTablesNet paging = null;
            HttpResponseMessage<DataTablesNet> returnValue = null;
            string[] data = null;

            int HuId = 0;
            int LobId = 0;
            string code = string.Empty;
            string name = string.Empty;
            try {
                paging = new DataTablesNet();
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');

                HuId = Convert.ToInt32(data[0].Split('=')[1]);
                LobId = Convert.ToInt32(data[1].Split('=')[1]);//TODO: remove
                code = data[2].Split('=')[1].NullSafeToString();
                name = data[3].Split('=')[1].NullSafeToString();

                var a = from b in AppContext.HuRooms
                        join d in AppContext.HuWards on b.WardId equals d.Id
                        join h in AppContext.RefHus on d.HospitalUnitId equals h.Id
                        join c in AppContext.HuLobRels on h.Id equals c.HospitalUnitId
                        join f in AppContext.RefLobs on c.LineOffBusinessUnitID equals f.Id
                        where d.HospitalUnitId == HuId
                        select b;

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.CodeValue.Contains(code) && p.NameValue.Contains(name));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.Id);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        #endregion

        #region Room
        [WebGet(UriTemplate = "room/")]
        public HttpResponseMessage<List<HuRoom>> GetListRoom() {
            HttpResponseMessage<List<HuRoom>> returnValue = null;
            List<HuRoom> listrooms = null;
            try {
                listrooms = (from h in AppContext.HuRooms select h).ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<HuRoom>>(statusCode);
                returnValue.Content = new ObjectContent<List<HuRoom>>(listrooms);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "room/{id}")]
        public HttpResponseMessage<HuRoom> GetRoomById(int id) {
            HttpResponseMessage<HuRoom> returnValue = null;
            HuRoom HuRoom = null;
            try {
                HuRoom = (from h in AppContext.HuRooms
                          where h.Id == id
                          select h)
                                   .First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<HuRoom>(statusCode);
                returnValue.Content = new ObjectContent<HuRoom>(HuRoom);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "room/ward/paging/")]
        public HttpResponseMessage<DataTablesNet> GetRoomByWardIDPaging(PostDataTablesNet pagingSearch) {
            DataTablesNet paging = null;
            HttpResponseMessage<DataTablesNet> returnValue = null;
            string[] data = null;

            int id = 0;
            string name = string.Empty;
            string code = string.Empty;

            try {
                paging = new DataTablesNet();
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');

                id = Convert.ToInt32(data[0].Split('=')[1]);
                code = data[1].Split('=')[1].NullSafeToString().Trim();
                name = data[2].Split('=')[1].NullSafeToString().Trim();

                var a = from b in AppContext.HuWards
                        join c in AppContext.HuRooms on b.Id equals c.WardId
                        where b.Id == id
                        select new {
                            c.Id,
                            c.NameValue,
                            c.CodeValue,
                            c.DescValue,
                        };

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.NameValue.Contains(name) && p.CodeValue.Contains(code));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.Id);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {

                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "room/")]
        public HttpResponseMessage<HuRoom> InsertRoom(HuRoom paramObject) {
            HttpResponseMessage<HuRoom> returnValue = null;
            try {
                if (AppContext.HuRooms.Where(d => d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Name of Room is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<HuRoom>(statusCode);
                returnValue.Content = new ObjectContent<HuRoom>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "room/")]
        public HttpResponseMessage<HuRoom> UpdateRoom(HuRoom paramObject) {
            HttpResponseMessage<HuRoom> returnValue = null;
            try {
                if (AppContext.HuRooms.Where(d => d.Id != paramObject.Id && d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Name of Room is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<HuRoom>(statusCode);
                returnValue.Content = new ObjectContent<HuRoom>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "room/")]
        public HttpResponseMessage<HuRoom> RemoveRoom(int id) {
            HttpResponseMessage<HuRoom> returnValue = null;
            HuRoom huroom = null;
            try {
                huroom = AppContext.HuRooms.Where(d => d.Id == id).First();

                AppContext.Entry(huroom).State = System.Data.EntityState.Deleted;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<HuRoom>(statusCode);
                returnValue.Content = new ObjectContent<HuRoom>(huroom);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "hospital/room/")]
        public HttpResponseMessage<DataTablesNet> GetAvailabeRoom(PostDataTablesNet pagingSearch) {
            DataTablesNet paging = null;
            HttpResponseMessage<DataTablesNet> returnValue = null;
            string[] param = null;

            string start = string.Empty;
            string end = string.Empty;
            int dayset = 0;
            string name = string.Empty;

            TimeSpan starttime = TimeSpan.Zero;
            TimeSpan endtime = TimeSpan.Zero;

            int HuId = Hospital.Id;

            try {
                paging = new DataTablesNet();
                paging.sEcho = pagingSearch.sEcho;
                param = pagingSearch.sSearch.Split(';');

                start = param[0].Split('=')[1].NullSafeToString();
                end = param[1].Split('=')[1].NullSafeToString();
                dayset = Convert.ToInt32(param[2].Split('=')[1]);
                name = param[3].Split('=')[1].NullSafeToString();

                starttime = TimeSpan.Parse(start);
                endtime = TimeSpan.Parse(end);

                starttime = new TimeSpan(starttime.Hours, starttime.Minutes + 1, 0);
                endtime = new TimeSpan(endtime.Hours, endtime.Minutes - 1, 0);

                var query = (from c in AppContext.HuRooms
                             join d in AppContext.DoctorSchedules on c.Id equals d.HospitalUnitRoomId
                             join e in AppContext.HuWards on c.WardId equals e.Id
                             where d.StartScheduleTime > starttime || d.EndScheduleTime > starttime
                             where d.StartScheduleTime < endtime
                             select d);


                query = query.Where(p => p.DaySet == dayset);


                var a = from b in AppContext.HuRooms
                        join e in AppContext.HuWards on b.WardId equals e.Id
                        join h in AppContext.RefHus on e.HospitalUnitId equals h.Id
                        where !(from c in query select c.HospitalUnitRoomId).Any(p => p == b.Id)
                        select new {
                            idroom = b.Id,
                            wardname = e.NameValue,
                            roomname = b.NameValue,
                            huroom = h.NameValue
                        };


                a = a.Where(p => p.huroom.Contains(name) || p.roomname.Contains(name));
                paging.iTotalRecords = a.Count();

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.idroom);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }

            returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
            returnValue.Content = new ObjectContent<DataTablesNet>(paging);

            if (paging == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            return returnValue;
        }

        #endregion

        #region Ward
        [WebGet(UriTemplate = "ward/")]
        public HttpResponseMessage<List<HuWard>> GetListWard() {
            HttpResponseMessage<List<HuWard>> returnValue = null;
            List<HuWard> listWard = null;
            try {
                listWard = (from h in AppContext.HuWards select h).ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<HuWard>>(statusCode);
                returnValue.Content = new ObjectContent<List<HuWard>>(listWard);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "ward/{id}")]
        public HttpResponseMessage<HuWard> GetWardById(int id) {
            HttpResponseMessage<HuWard> returnValue = null;
            HuWard huward = null;
            try {
                huward = (from h in AppContext.HuWards
                          where h.Id == id
                          select h).First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<HuWard>(statusCode);
                returnValue.Content = new ObjectContent<HuWard>(huward);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "ward/hospital/paging/")]
        public HttpResponseMessage<DataTablesNet> GetWardByHuIDPaging(PostDataTablesNet pagingSearch) {
            DataTablesNet paging = null;
            HttpResponseMessage<DataTablesNet> returnValue = null;
            string[] data = null;

            int id = 0;
            string name = string.Empty;


            try {
                paging = new DataTablesNet();
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');

                id = Convert.ToInt32(data[0].Split('=')[1]);
                name = data[1].Split('=')[1].NullSafeToString().Trim();


                var a = from b in AppContext.HuWards
                        join c in AppContext.RefHus on b.HospitalUnitId equals c.Id
                        where b.HospitalUnitId == id
                        select new {
                            b.Id,
                            b.NameValue,
                            b.DescValue,
                        };

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.NameValue.Contains(name));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.Id);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {

                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "ward/")]
        public HttpResponseMessage<HuWard> InsertWard(HuWard paramObject) {
            HttpResponseMessage<HuWard> returnValue = null;
            try {
                if (AppContext.HuWards.Where(d => d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Name of Ward is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<HuWard>(statusCode);
                returnValue.Content = new ObjectContent<HuWard>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "ward/")]
        public HttpResponseMessage<HuWard> UpdateWard(HuWard paramObject) {
            HttpResponseMessage<HuWard> returnValue = null;
            try {
                if (AppContext.HuWards.Where(d => d.Id != paramObject.Id && d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Name of Ward is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<HuWard>(statusCode);
                returnValue.Content = new ObjectContent<HuWard>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "ward/")]
        public HttpResponseMessage<HuWard> RemoveWard(int id) {
            HttpResponseMessage<HuWard> returnValue = null;
            HuWard huward = null;
            try {
                huward = AppContext.HuWards.Where(d => d.Id == id).First();

                AppContext.Entry(huward).State = System.Data.EntityState.Deleted;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<HuWard>(statusCode);
                returnValue.Content = new ObjectContent<HuWard>(huward);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }
        #endregion

        #region Bed
        [WebGet(UriTemplate = "bed/")]
        public HttpResponseMessage<List<HuBed>> GetListBed() {
            HttpResponseMessage<List<HuBed>> returnValue = null;
            List<HuBed> listHuBed = null;
            try {
                listHuBed = (from h in AppContext.HuBeds select h).ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<HuBed>>(statusCode);
                returnValue.Content = new ObjectContent<List<HuBed>>(listHuBed);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "bed/{id}")]
        public HttpResponseMessage<HuBed> GetBedById(int id) {
            HttpResponseMessage<HuBed> returnValue = null;
            HuBed Hubed = null;
            try {
                Hubed = (from h in AppContext.HuBeds
                          where h.Id == id
                          select h).First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<HuBed>(statusCode);
                returnValue.Content = new ObjectContent<HuBed>(Hubed);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "bed/room/paging/")]
        public HttpResponseMessage<DataTablesNet> GetBedByRoomIdPaging(PostDataTablesNet pagingSearch) {
            DataTablesNet paging = null;
            HttpResponseMessage<DataTablesNet> returnValue = null;
            string[] data = null;

            int id = 0;
            string name = string.Empty;


            try {
                paging = new DataTablesNet();
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');

                id = Convert.ToInt32(data[0].Split('=')[1]);
                name = data[1].Split('=')[1].NullSafeToString().Trim();


                var a = from b in AppContext.HuBeds
                        join c in AppContext.HuRooms on b.RoomId equals c.Id
                        where b.RoomId == id
                        select new {
                            b.Id,
                            b.NameValue
                        };

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.NameValue.Contains(name));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.Id);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {

                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "bed/")]
        public HttpResponseMessage<HuBed> InsertBed(HuBed paramObject) {
            HttpResponseMessage<HuBed> returnValue = null;
            try {
                if (AppContext.HuBeds.Where(d => d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<HuBed>(statusCode);
                returnValue.Content = new ObjectContent<HuBed>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "bed/")]
        public HttpResponseMessage<HuBed> UpdateBed(HuBed paramObject) {
            HttpResponseMessage<HuBed> returnValue = null;
            try {
                if (AppContext.HuBeds.Where(d => d.Id != paramObject.Id && d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<HuBed>(statusCode);
                returnValue.Content = new ObjectContent<HuBed>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "bed/")]
        public HttpResponseMessage<HuBed> RemoveBed(int id) {
            HttpResponseMessage<HuBed> returnValue = null;
            HuBed hubed = null;
            try {
                hubed = AppContext.HuBeds.Where(d => d.Id == id).First();

                AppContext.Entry(hubed).State = System.Data.EntityState.Deleted;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<HuBed>(statusCode);
                returnValue.Content = new ObjectContent<HuBed>(hubed);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }
        #endregion

        #region Line Of Bussiness
        [WebGet(UriTemplate = "lineofbusiness/")]
        public HttpResponseMessage<IQueryable> GetLoB() {
            HttpResponseMessage<IQueryable> returnValue = null;
            IQueryable data = null;
            int Huid = Hospital.Id;
            try {
                data = from h in AppContext.RefLobs
                       join i in AppContext.HuLobRels on h.Id equals i.LineOffBusinessUnitID
                       join j in AppContext.RefHus on i.HospitalUnitId equals j.Id
                       where i.HospitalUnitId == Huid
                       select new {
                           Value = i.Id,
                           Name = h.CodeValue
                       };

                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<IQueryable>(statusCode);
                returnValue.Content = new ObjectContent<IQueryable>(data);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);

            }
            return returnValue;
        }

        [WebGet(UriTemplate = "lineofbusiness/list/")]
        public HttpResponseMessage<List<RefLob>> GetListLoB() {
            HttpResponseMessage<List<RefLob>> returnValue = null;
            List<RefLob> listRefLoB = null;
            int Huid = Hospital.Id;
            try {
                listRefLoB = (from h in AppContext.RefLobs
                              select h).ToList();

                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<RefLob>>(statusCode);
                returnValue.Content = new ObjectContent<List<RefLob>>(listRefLoB);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);

            }
            return returnValue;
        }
        #endregion

        #region Specialist
        [WebGet(UriTemplate = "groupspecialist/")]
        public HttpResponseMessage<List<RefSpecialistGroup>> GetListGroupSpecialist() {
            HttpResponseMessage<List<RefSpecialistGroup>> returnValue = null;
            List<RefSpecialistGroup> listGroupSpec = null;
            try {
                listGroupSpec = AppContext.RefSpecialistGroups.ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<RefSpecialistGroup>>(statusCode);
                returnValue.Content = new ObjectContent<List<RefSpecialistGroup>>(listGroupSpec);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebGet(UriTemplate = "groupspecialist/{id}")]
        public HttpResponseMessage<RefSpecialistGroup> GetGroupSpecialistById(int id) {
            HttpResponseMessage<RefSpecialistGroup> returnValue = null;
            RefSpecialistGroup groupSpecialist = null;
            try {
                groupSpecialist = AppContext.RefSpecialistGroups.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefSpecialistGroup>(statusCode);
                returnValue.Content = new ObjectContent<RefSpecialistGroup>(groupSpecialist);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "groupspecialist/paging/")]
        public HttpResponseMessage<DataTablesNet> GetGroupSpecAllPost(PostDataTablesNet pagingSearch) {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();
            string[] data = null;
            string namevalue = string.Empty;
            string descvalue = string.Empty;

            try {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');
                namevalue = data[0].Split('=')[1].NullSafeToString().Trim();
                descvalue = data[1].Split('=')[1].NullSafeToString().Trim();

                var a = from b in AppContext.RefSpecialistGroups select b;

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.DescValue.Contains(descvalue) && p.NameValue.Contains(namevalue));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.Id);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "groupspecialist/")]
        public HttpResponseMessage<RefSpecialistGroup> InsertGroupSpecialist(RefSpecialistGroup paramObject) {
            HttpResponseMessage<RefSpecialistGroup> returnValue = null;
            try {
                if (AppContext.RefSpecialistGroups.Where(d => d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Name of Group Specialist is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefSpecialistGroup>(statusCode);
                returnValue.Content = new ObjectContent<RefSpecialistGroup>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "groupspecialist/")]
        public HttpResponseMessage<RefSpecialistGroup> UpdateGroupSpecialist(RefSpecialistGroup paramObject) {
            HttpResponseMessage<RefSpecialistGroup> returnValue = null;
            try {
                if (AppContext.RefSpecialistGroups.Where(d => d.NameValue == paramObject.NameValue && d.Id != paramObject.Id).Count() > 0) {
                    detailMessage = "Name of Group Specialist is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefSpecialistGroup>(statusCode);
                returnValue.Content = new ObjectContent<RefSpecialistGroup>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "groupspecialist/")]
        public HttpResponseMessage<RefSpecialistGroup> DeleteGroupSpecialist(int id) {
            HttpResponseMessage<RefSpecialistGroup> returnValue = null;
            RefSpecialistGroup RefSpecGroup = null;
            try {
                RefSpecGroup = AppContext.RefSpecialistGroups.Where(d => d.Id == id).First();

                AppContext.Entry(RefSpecGroup).State = System.Data.EntityState.Deleted;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefSpecialistGroup>(statusCode);
                returnValue.Content = new ObjectContent<RefSpecialistGroup>(RefSpecGroup);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebGet(UriTemplate = "specialist/")]
        public HttpResponseMessage<List<RefSpecialist>> GetListSpecialist() {
            HttpResponseMessage<List<RefSpecialist>> returnValue = null;
            List<RefSpecialist> listSpec = null;
            try {
                listSpec = AppContext.RefSpecialists.ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<RefSpecialist>>(statusCode);
                returnValue.Content = new ObjectContent<List<RefSpecialist>>(listSpec);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebGet(UriTemplate = "specialist/{id}")]
        public HttpResponseMessage<RefSpecialist> GetSpecialistById(int id) {
            HttpResponseMessage<RefSpecialist> returnValue = null;
            RefSpecialist specialist = null;
            try {
                specialist = AppContext.RefSpecialists.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefSpecialist>(statusCode);
                returnValue.Content = new ObjectContent<RefSpecialist>(specialist);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "specialist/group/paging")]
        public HttpResponseMessage<DataTablesNet> GetSpecialistByGroupId(PostDataTablesNet pagingSearch) {
            HttpResponseMessage<DataTablesNet> returnValue = null;
            DataTablesNet paging = new DataTablesNet();
            string[] data = null;
            int groupid = 0;
            string namevalue = string.Empty;
            string codevalue = string.Empty;

            try {
                data = pagingSearch.sSearch.Split(';');
                groupid = Convert.ToInt32(data[0].Split('=')[1]);
                namevalue = data[1].Split('=')[1].NullSafeToString().Trim();
                codevalue = data[2].Split('=')[1].NullSafeToString().Trim();

                paging.sEcho = pagingSearch.sEcho;

                var a = from b in AppContext.RefSpecialists
                        join c in AppContext.RefSpecialistGroups on b.GroupSpecialistId equals c.Id
                        where b.GroupSpecialistId == groupid
                        select b;

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.CodeValue.Contains(codevalue) && p.NameValue.Contains(namevalue));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.Id);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "specialist/")]
        public HttpResponseMessage<RefSpecialist> InsertSpecialist(RefSpecialist paramObject) {
            HttpResponseMessage<RefSpecialist> returnValue = null;
            try {
                if (AppContext.RefSpecialists.Where(d => d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Name of Specialist is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefSpecialist>(statusCode);
                returnValue.Content = new ObjectContent<RefSpecialist>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "specialist/")]
        public HttpResponseMessage<RefSpecialist> UpdateSpecialist(RefSpecialist paramObject) {
            HttpResponseMessage<RefSpecialist> returnValue = null;
            try {
                if (AppContext.RefSpecialists.Where(d => d.Id != paramObject.Id && d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Name of Group Specialist is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefSpecialist>(statusCode);
                returnValue.Content = new ObjectContent<RefSpecialist>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "specialist/")]
        public HttpResponseMessage<RefSpecialist> DeleteSpecialist(int id) {
            HttpResponseMessage<RefSpecialist> returnValue = null;
            RefSpecialist RefSpec = null;
            try {
                RefSpec = AppContext.RefSpecialists.Where(d => d.Id == id).First();


                AppContext.Entry(RefSpec).State = System.Data.EntityState.Modified;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefSpecialist>(statusCode);
                returnValue.Content = new ObjectContent<RefSpecialist>(RefSpec);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebGet(UriTemplate = "subspecialist/")]
        public HttpResponseMessage<List<RefSubSpecialist>> GetListSubSpecialist() {
            HttpResponseMessage<List<RefSubSpecialist>> returnValue = null;
            List<RefSubSpecialist> listSubSpec = null;
            try {
                listSubSpec = AppContext.RefSubSpecialists.ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<RefSubSpecialist>>(statusCode);
                returnValue.Content = new ObjectContent<List<RefSubSpecialist>>(listSubSpec);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebGet(UriTemplate = "subspecialist/{id}")]
        public HttpResponseMessage<RefSubSpecialist> GetListSubSpecialistById(int id) {
            HttpResponseMessage<RefSubSpecialist> returnValue = null;
            RefSubSpecialist subspecialist = null;
            try {
                subspecialist = AppContext.RefSubSpecialists.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefSubSpecialist>(statusCode);
                returnValue.Content = new ObjectContent<RefSubSpecialist>(subspecialist);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "subspecialist/spec/paging/")]
        public HttpResponseMessage<DataTablesNet> GetSubSpecialistAllPost(PostDataTablesNet pagingSearch) {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();

            int subspecid = 0;
            string[] data = null;
            string namevalue = string.Empty;
            string codevalue = string.Empty;

            try {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');
                subspecid = Convert.ToInt32(data[0].Split('=')[1]);
                namevalue = data[1].Split('=')[1].NullSafeToString().Trim();
                codevalue = data[2].Split('=')[1].NullSafeToString().Trim();

                var a = from b in AppContext.RefSubSpecialists
                        join c in AppContext.RefSpecialists on b.SpecialistId equals c.Id
                        where b.SpecialistId == subspecid
                        select b;

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.CodeValue.Contains(codevalue) && p.NameValue.Contains(namevalue));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.Id);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "subspecialist/")]
        public HttpResponseMessage<RefSubSpecialist> InsertSubSpecialist(RefSubSpecialist paramObject) {
            HttpResponseMessage<RefSubSpecialist> returnValue = null;
            try {
                if (AppContext.RefSubSpecialists.Where(d => d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Name of Specialist is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefSubSpecialist>(statusCode);
                returnValue.Content = new ObjectContent<RefSubSpecialist>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "subspecialist/")]
        public HttpResponseMessage<RefSubSpecialist> UpdateSubSpecialist(RefSubSpecialist paramObject) {
            HttpResponseMessage<RefSubSpecialist> returnValue = null;
            try {
                if (AppContext.RefSubSpecialists.Where(d => d.Id != paramObject.Id && d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Name of Group Specialist is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefSubSpecialist>(statusCode);
                returnValue.Content = new ObjectContent<RefSubSpecialist>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "subspecialist/")]
        public HttpResponseMessage<RefSubSpecialist> DeleteSubSpecialist(int id) {
            HttpResponseMessage<RefSubSpecialist> returnValue = null;
            RefSubSpecialist RefSubSpec = null;
            try {
                RefSubSpec = AppContext.RefSubSpecialists.Where(d => d.Id == id).First();

                AppContext.Entry(RefSubSpec).State = System.Data.EntityState.Deleted;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefSubSpecialist>(statusCode);
                returnValue.Content = new ObjectContent<RefSubSpecialist>(RefSubSpec);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }
        #endregion

        #region RefUOMType
        [WebGet(UriTemplate = "uomtype/{id}")]
        public HttpResponseMessage<RefUomType> GetUomTypeById(int id)
        {
            HttpResponseMessage<RefUomType> returnValue = null;
            RefUomType refuomtype = null;
            try
            {
                refuomtype = AppContext.RefUomTypes.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<RefUomType>(statusCode);
                returnValue.Content = new ObjectContent<RefUomType>(refuomtype);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "uomtype/paging/")]
        public HttpResponseMessage<DataTablesNet> GetUomTypeAllPost(PostDataTablesNet pagingSearch)
        {

            DataTablesNet paging = null;
            HttpResponseMessage<DataTablesNet> returnValue = null;
            string[] param = null;


            string name = string.Empty;
            string code = string.Empty;


            try
            {
                paging = new DataTablesNet();
                paging.sEcho = pagingSearch.sEcho;
                param = pagingSearch.sSearch.Split(';');


                name = param[0].Split('=')[1].NullSafeToString();
                code = param[1].Split('=')[1].NullSafeToString();

               
                var a = from b in AppContext.RefUomTypes
                        select b;
                        

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.NameValue.Contains(name) && p.CodeValue.Contains(code));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.NameValue);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
            returnValue.Content = new ObjectContent<DataTablesNet>(paging);

            if (paging == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "uomtype/")]
        public HttpResponseMessage<RefUomType> InsertUomType(RefUomType paramObject)
        {
            HttpResponseMessage<RefUomType> returnValue = null;
            try
            {
                if (AppContext.RefUomTypes.Where(d => d.CodeValue == paramObject.CodeValue).Count() > 0)
                {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                }
                else
                {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<RefUomType>(statusCode);
                returnValue.Content = new ObjectContent<RefUomType>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "uomtype/")]
        public HttpResponseMessage<RefUomType> UpdateUomType(RefUomType paramObject)
        {
            HttpResponseMessage<RefUomType> returnValue = null;
            try
            {
                if (AppContext.RefUomTypes.Where(d =>  d.CodeValue == paramObject.CodeValue && d.Id != paramObject.Id).Count() > 0)
                {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                }
                else
                {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<RefUomType>(statusCode);
                returnValue.Content = new ObjectContent<RefUomType>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "uomtype/")]
        public HttpResponseMessage<RefUomType> DeleteUomTypeByID(int id)
        {
            HttpResponseMessage<RefUomType> returnValue = null;
            RefUomType refuomtype = null;
            try
            {
                refuomtype = AppContext.RefUomTypes.Where(d => d.Id == id).First();

                AppContext.Entry(refuomtype).State = System.Data.EntityState.Deleted;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<RefUomType>(statusCode);
                returnValue.Content = new ObjectContent<RefUomType>(refuomtype);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }
        #endregion


        void IDisposable.Dispose() {
            try {
                base.Dispose();
                GC.SuppressFinalize(this);
            } catch { }
        }
    }
}